package com.andy.nicefi.action;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.andy.nicefi.bean.User;
import com.andy.nicefi.service.UserService;
import com.andy.nicefi.util.Mail;
import com.andy.nicefi.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	
	@Autowired
	private UserService userService;
	
	private User user;
	private String id;
	private List<User> userList = new ArrayList<User>();
	private String message;
	
	public String execute(){
		return "success";
	}
	
	public void validateSave() {
		String username = user.getUsername();
		if(userService.existUser(" from User where username='" + username+"'"))
			addFieldError("user.username", getText("userName.dumplicated"));
		
		String email = user.getEmail();
		if(userService.existUser(" from User where email='" + email+"'"))
			addFieldError("user.email", getText("email.dumplicated"));
	}
	
	public String save(){
		String pwd = user.getPassword();
		userService.saveUser(user);
		Mail mail = new Mail(user.getEmail(), user.getUsername(), pwd);
		String subject = getText("mail.subject.activate");
		String content = getText("mail.content.activate");
		MessageFormat mft = new MessageFormat(content);
		String[] replaced = new String[] {user.getUsername(), user.getEmail(), pwd, user.getEmail()};
		content = mft.format(replaced);
		mail.setContent(content);
		mail.setSubject(subject);
		try {
			mail.send();
		} catch (Exception e) {
			message = getText("register.sendmail.failed");
		}
		userList = userService.getAllUserList();
		return "save";
	}
	
	public String sendMail(){
		user = userService.getUser("from User where email='"+user.getEmail()+"'");
		Mail mail = new Mail(user.getEmail(), user.getUsername(), user.getPassword());
		String subject = getText("mail.subject.activate");
		String content = getText("mail.content.activate");
		MessageFormat mft = new MessageFormat(content);
		String[] replaced = new String[] {user.getUsername(), user.getEmail(), user.getPassword(), user.getEmail()};
		content = mft.format(replaced);
		mail.setContent(content);
		mail.setSubject(subject);
		try {
			mail.send();
		} catch (Exception e) {
			message = getText("register.sendmail.failed");
		}
		return "success";
	}
	
	public String resetPassword(){
		String newpwd = StringUtil.getRandomString();
		String newmd5 = StringUtil.getMD5String(newpwd);
		user = userService.getUser("from User where email='"+user.getEmail()+"'");
		user.setPassword(newmd5);
		userService.updateUser(user);
		Mail mail = new Mail(user.getEmail(), user.getUsername(), newpwd);
		String subject = getText("mail.subject.resetPassword");
		String content = getText("mail.content.resetPassword");
		MessageFormat mft = new MessageFormat(content);
		String[] replaced = new String[] {user.getUsername(), user.getEmail(), newpwd};
		content = mft.format(replaced);
		mail.setContent(content);
		mail.setSubject(subject);
		try {
			mail.send();
		} catch (Exception e) {
			message = getText("register.sendmail.failed");
		}
		return "success";
	}
	
	public String activate(){
		try {
			userService.activate(user.getEmail());
			message = getText("activate.success");
			return "activate";
		} catch (Exception e) {
			message = e.getMessage();
			return "failed";
		}
	}
	
	public String list(){
		userList = userService.getAllUserList();
		return "list";
	}
	
	public String edit(){
		return "edit";
	}
	
	public String delete(){
		userService.deleteUser(id);
		userList = userService.getAllUserList();
		return "delete";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
